﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Big_Data.View
{
    public partial class FrmSataionType : Form
    {
        DataBase userDB = new DataBase();

        public FrmSataionType()
        {
            InitializeComponent();

            //统一页面窗口的属性，例如窗口打开都是页面最中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }

        private void FrmSataionType_Load(object sender, EventArgs e)
        {
            //初始化绑定数据
            bindStationTypeDate();
        }

        private void bindStationTypeDate()
        {
            try
            {
                string sqlstr = "select * from S_stationtype";
                DataTable table = userDB.show(sqlstr);
                this.dgwStationType.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                userDB.conn_close();
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //查询code是否存在
                string sqlExists = string.Format("select * from S_stationtype where  Sta_usercode='{0}'", this.txtCode.Text);
                DataSet ds = userDB.getDs(sqlExists, "S_stationtype");

                //如果能查询到数据，则为编辑，否则新增
                if (ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string sqlstr = string.Format("update S_stationtype set Sta_username='{0}' where  Sta_usercode='{1}'",
                        this.txtName.Text, this.txtCode.Text);
                    userDB.Distributortableinsert(sqlstr);
                    MessageBox.Show("修改成功！");
                }
                else
                {
                    string sqlstr = string.Format("insert into S_stationtype(Sta_username,Sta_usercode) values('{0}','{1}')",
                          this.txtName.Text, this.txtCode.Text);
                    userDB.Distributortableinsert(sqlstr);
                    MessageBox.Show("新增成功！");
                }

                //编辑完毕，重新绑定刷新数据
                bindStationTypeDate();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据插入错误" + ex.Message);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndelete_Click(object sender, EventArgs e)
        {
            DialogResult dialogClose = MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogClose == DialogResult.OK)
            {
                try
                {
                    string Sta_usercode = txtCode.Text.Trim();
                    string delete = "DELETE FROM S_stationtype WHERE Sta_usercode ='" + Sta_usercode + "'";
                    userDB.Distributortabledelete(delete);

                    //编辑完毕，重新绑定刷新数据
                    bindStationTypeDate();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据删除错误" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 列表行记录选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgwStationType_SelectionChanged(object sender, EventArgs e)
        {
            //判断是否选择了行数据，如果选择项为0，则什么都不处理。
            if (dgwStationType.SelectedRows.Count == 0)
            {
                return;
            }

            //将数据绑定到左侧文本框中
            DataGridViewRow currentRow = dgwStationType.CurrentRow;
            if (currentRow.Cells[0].Value != null)
            {
                this.txtCode.Text = currentRow.Cells[0].Value.ToString();
                this.txtName.Text = currentRow.Cells[1].Value.ToString();
            }
        }
    }
}
